package com.daikit.daikit4gxt.client.ui.editor.advanced;

import java.util.ArrayList;
import java.util.List;

import com.daikit.commons.shared.utils.DkCollectionUtils;
import com.daikit.commons.shared.utils.DkStringUtils;
import com.daikit.daikit4gxt.client.DkBaseMain;
import com.daikit.daikit4gxt.client.editor.DkListStoreRemovableEditor;
import com.daikit.daikit4gxt.client.labelprovider.DkEnumLabelProvider;
import com.daikit.daikit4gxt.client.propertyaccess.DkEnumEditDeletePropertyAccess;
import com.daikit.daikit4gxt.client.ui.editor.field.DkComboBox;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.form.ComboBox;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;


/**
 * Special {@link DkReferenceEditorGrid} for Enums
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <T>
 */
public abstract class DkReferenceEnumEditorGrid<T extends Enum<T>> extends DkReferenceEditorGrid<T>
{

	/**
	 * Constructor
	 * 
	 * @param listStore
	 *           the field {@link ListStore} (from a {@link DkListStoreRemovableEditor} usually)
	 * @param propertyAccess
	 *           the {@link DkEnumEditDeletePropertyAccess}
	 */
	public DkReferenceEnumEditorGrid(final ListStore<T> listStore, final DkEnumEditDeletePropertyAccess<T> propertyAccess)
	{
		super(listStore, DkReferenceEnumEditorGrid.<T> getColumnConfigs(), propertyAccess);
	}

	private static <T extends Enum<?>> List<ColumnConfig<T, ?>> getColumnConfigs()
	{
		final List<ColumnConfig<T, ?>> columnConfigs = new ArrayList<ColumnConfig<T, ?>>();

		final ColumnConfig<T, String> columnValue = new ColumnConfig<T, String>(new ValueProvider<T, String>()
		{
			@Override
			public String getValue(final T object)
			{
				return DkStringUtils.toString(object);
			}

			@Override
			public void setValue(final T object, final String value)
			{
				// Not used
			}

			@Override
			public String getPath()
			{
				return "";
			}
		}, 250, DkBaseMain.i18n().label_value());

		columnConfigs.add(columnValue);

		return columnConfigs;
	}


	@Override
	public void invalidateUi()
	{
		getSelectionField().setEnabled(DkBaseMain.model().isLogged());
	}

	@Override
	protected ComboBox<T> createSelectionField()
	{
		return new DkComboBox<T>(DkCollectionUtils.asList(getEnumValues()), new DkEnumLabelProvider<T>());
	}

	protected abstract T[] getEnumValues();

}
